package com.Product;
import  com.model.inf04.inf04_1;
import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import com.enclose.dataBasePool;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.exceptions.CJConnectionFeatureNotAvailableException;

@WebServlet("/Product/add_shoppingCar")
public class add_shoppingCar extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String str;
        ObjectMapper objectMapper = new ObjectMapper();
        //1.请求参数
        Integer productId = 0;
        Integer userId = 0;
        if(req.getParameter("productId") != null){
            productId = Integer.valueOf("productId");
        }
        if(req.getParameter("userId") != null){
            userId = Integer.valueOf("userId");
        }
        //2.访问数据库
        Statement statement = null;
        try {
            dataBasePool dataBasePool = new dataBasePool();
             statement = dataBasePool.getStatement();
            String sql = "DELETE shopping_car \n" +
                    "WHERE user_id= 1 AND product_id = 1;";
            //存在
            int result = statement.executeUpdate(sql);
            //不存在
            statement.close();
            if (result<=0){
                Statement statement1 = dataBasePool.getStatement();
                String sql1 ="INSERT INTO shopping_car (user_id,product_id) VALUES (1,1)";
                int result1 = statement.executeUpdate(sql1);
                statement1.close();
            }

        } catch (Exception e) {
            str = objectMapper.writeValueAsString(new inf04_1(500,"defult！"));
            throw new RuntimeException(e);
        }finally {

            str =  objectMapper.writeValueAsString(new inf04_1(200,"success！"));
            resp.getWriter().write(str);
        }

        //3.像前端输出



    }
}
